package client.service;


import model.Message;

import client.main.Client_main;
import model.MsgType;
import model.User;
import javafx.scene.control.Alert;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.regex.Pattern;

import com.google.gson.Gson;

//对用户输入的登录信息进行校验，符合格式后发送到服务器，并接收校验结果将其返回
public class LoginUser {
    private Socket client;
    private DataOutputStream output;
    private DataInputStream input;

    public LoginUser() {        
        try {
            client = new Socket(Client_main.ip, 12345);
            output = new DataOutputStream(client.getOutputStream());
            input = new DataInputStream(client.getInputStream());
            System.out.println("初始化LoginUser");
        } catch (IOException e) {
            System.out.println("连接服务器失败！");
            e.printStackTrace();
        }
    }

    //检测用户输入信息是否合理，合理则将登录信息封存到对象中返回，不合理则显示提示信息
    private User checkLoginInfo(String uid, String pwd) {
        User user = null;
        String pattern = "[0-9]{3,10}";
        String patterns = "[a-zA-Z0-9]{1,10}";
        if (!uid.equals("") && !pwd.equals("")) {
            if (Pattern.matches(pattern, uid)) {
                if (Pattern.matches(patterns, pwd)) {
                    user = new User();
                    user.setPassword(pwd);
                    user.setId(Integer.parseInt(uid));
                } else {//密码不合法
                    Alert alert = new Alert(Alert.AlertType.WARNING);
                    alert.setContentText("密码为1到10位英文字母或数字，请重新输入！");
                    alert.show();
                }
            } else {//账号不合法
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText("账号为3到10位数字，请重新输入！");
                alert.show();
            }
        } else if (uid.equals("")) {//账号为空
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setContentText("请输入账号！");
            alert.show();
        } else if (pwd.equals("")) {//密码为空
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setContentText("请输入密码！");
            alert.show();
        }
        return user;
    }

  //将通过校验的登录信息发送到服务器并将得到的消息包返回(包含当前用户的所有好友)
    public Message sendLoginInfoToServer(String uid, String pwd) {
        User u = checkLoginInfo(uid, pwd);
        Message mes = new Message();
        if (u != null) {
            try {
                Gson gson = new Gson();
                
                mes.setUserID(u.getId());
                mes.setText(pwd);
                mes.setType(MsgType.LOGIN);
                String json = gson.toJson(mes);
                output.writeUTF(json);
                System.out.println(u.getId());
                
                String retmes = input.readUTF();
                mes = gson.fromJson(retmes, Message.class);
                
                if(mes.getType() == MsgType.LOGIN_SUCCEED ) {
                	                	
                	client.close();
                	System.out.println("success");
                	return mes;
                }else if (mes.getType() == MsgType.LOGIN_FAILED_WRONG) {
                	Alert alert = new Alert(Alert.AlertType.WARNING);
                    alert.setContentText("账号或密码输入错误，请重新输入！");
                   alert.show();
                   
                }else if (mes.getType() == MsgType.ALREADY_LOGIN) {
                	Alert alert = new Alert(Alert.AlertType.WARNING);
                    alert.setContentText("该用户已登录，请勿重复操作！");
                    alert.show();
                }else if (mes.getType() == MsgType.LOGIN_FAILED_NOACCOUNT) {
                	Alert alert = new Alert(Alert.AlertType.WARNING);
                    alert.setContentText("没有此账号！");
                    alert.show();
                }               	                            
         } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return mes;
    }
}



